/**
 * AttendAction.java
 */
package com.prs.crm.action.official;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.oa.CheckWork;
import com.prs.crm.domain.oa.GoOut;
import com.prs.crm.domain.sys.CheckingSet;
import com.prs.crm.service.oa.CheckWorkService;
import com.prs.crm.service.oa.GoOutService;
import com.prs.crm.service.sys.CheckingSetService;

/**
 * @author ZhuDongMei
 * @E-mail:1223984339@qq.com
 * @qq:1223984339
 * @version 创建时间：2011-3-16 上午11:12:28 简单说明
 */

public class AttendAction extends BaseAction {
  
  private static final long serialVersionUID = 1L;
  private GoOut goOut;
  private GoOutService goOutService;
  private PaginationSupport pagination;
  private CheckingSet checkingSet;
  private CheckingSetService checkingSetService;
  private CheckWork checkWork = new CheckWork();
  private CheckWorkService checkWorkService;
  private String d1;
  private String d2;
  private String d3;
  private String d4;
  private String inserCode;
  private Integer persons;
  
public Integer getPersons() {
	return persons;
}
public void setPersons(Integer persons) {
	this.persons = persons;
}
public String getInserCode() {
	return inserCode;
}
public void setInserCode(String inserCode) {
	this.inserCode = inserCode;
}
public String getD2() {
	return d2;
}
public void setD2(String d2) {
	this.d2 = d2;
}
public String getD3() {
	return d3;
}
public void setD3(String d3) {
	this.d3 = d3;
}
public String getD4() {
	return d4;
}
public void setD4(String d4) {
	this.d4 = d4;
}
public String getD1() {
	return d1;
}
public void setD1(String d1) {
	this.d1 = d1;
}
public CheckWork getCheckWork() {
	return checkWork;
}
public void setCheckWork(CheckWork checkWork) {
	this.checkWork = checkWork;
}
public CheckWorkService getCheckWorkService() {
	return checkWorkService;
}
public void setCheckWorkService(CheckWorkService checkWorkService) {
	this.checkWorkService = checkWorkService;
}
public CheckingSetService getCheckingSetService() {
	return checkingSetService;
}
public void setCheckingSetService(CheckingSetService checkingSetService) {
	this.checkingSetService = checkingSetService;
}
public CheckingSet getCheckingSet() {
	return checkingSet;
}
public void setCheckingSet(CheckingSet checkingSet) {
	this.checkingSet = checkingSet;
}
public GoOut getGoOut() {
	return goOut;
}
public void setGoOut(GoOut goOut) {
	this.goOut = goOut;
}
public GoOutService getGoOutService() {
	return goOutService;
}
public void setGoOutService(GoOutService goOutService) {
	this.goOutService = goOutService;
}
public PaginationSupport getPagination() {
	return pagination;
}
public void setPagination(PaginationSupport pagination) {
	this.pagination = pagination;
}
@Action("listAttend")
public String listAttend(){
	persons=AppContext.getAccountContext().currentUser().getId();
	Date dt=new Date();
	SimpleDateFormat sf=new SimpleDateFormat("yyyyMMdd");
	String codes=sf.format(dt);
	this.checkWork=this.getCheckWorkService().getCode(codes,persons);
	if (checkWork==null) {
		checkWork=new CheckWork();
		checkWork.setCode(codes);
		checkWork.setPersons(persons);
		this.getCheckWorkService().save(checkWork);
	}
	this.checkingSet = this.getCheckingSetService().getCheckingSet("cs01");
	return SUCCESS;
}

@Action("listAttends")
public String listAttends(){
	persons=AppContext.getAccountContext().currentUser().getId();
	Date dt=new Date();
	SimpleDateFormat sf=new SimpleDateFormat("yyyyMMdd");
	String codes=sf.format(dt);
	this.checkWork=this.getCheckWorkService().getCode(codes,persons);
	persons=AppContext.getAccountContext().currentUser().getId();
	if (checkWork==null) {
		checkWork=new CheckWork();
		checkWork.setCode(codes);
		checkWork.setPersons(persons);
		this.getCheckWorkService().save(checkWork);
		checkWork.setPersons(persons);
	}
	this.checkingSet = this.getCheckingSetService().getCheckingSet("cs01");

	return SUCCESS;
}
@Action(value="saveCheckWork", results = {
	@Result(name = "success", type = "redirect", location = "listAttend?inserCode=${checkWork.getCode()}"),
	@Result(name = "error", type = "chain", location = "listAttend?inserCode=${checkWork.getCode()}") })
	public String saveCheckWork() throws ParseException{
	persons=AppContext.getAccountContext().currentUser().getId();
	Date dt2=new Date();
	SimpleDateFormat sf1=new SimpleDateFormat("yyyyMMdd");
	String codes=sf1.format(dt2);
	this.checkWork=this.getCheckWorkService().getCode(codes,persons);
	if (checkWork!=null) {
		checkWork.setMorState1(0);
		checkWork.setPersons(persons);
		checkWork.setMorningInTime(new Date());
		this.getCheckWorkService().save(checkWork);
	}
	Date dt=new Date();
	SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
	String time=sf.format(dt)+" "+checkingSet.getForenoonWorkTime();
	Date dt1=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(time);
	if (dt1.after(checkWork.getMorningInTime())) {
		checkWork.setMorState1(0);
	}else{
		checkWork.setMorState1(1);//迟到
		if (checkWork.getRemark()==null || "".equals(checkWork.getRemark().trim())) {
			checkWork.setRemark("迟到");
		}else{
			checkWork.setRemark(checkWork.getRemark()+" "+"迟到");
		}
	}
	this.getCheckWorkService().submit(checkWork);
	return SUCCESS;
	}
@Action(value="saveCheckWork2", results = {
		@Result(name = "success", type = "redirect", location = "listAttend?inserCode=${checkWork.getCode()}"),
		@Result(name = "error", type = "chain", location = "listAttend?inserCode=${checkWork.getCode()}") })
		public String saveCheckWork2() throws ParseException{
		persons=AppContext.getAccountContext().currentUser().getId();
		Date dt2=new Date();
		SimpleDateFormat sf1=new SimpleDateFormat("yyyyMMdd");
		String codes=sf1.format(dt2);
		this.checkWork=this.getCheckWorkService().getCode(codes,persons);
		if (checkWork!=null) {
			checkWork.setMorState2(0);//正常
			checkWork.setMorningOutTime(new Date());
			checkWork.setPersons(persons);
			this.getCheckWorkService().save(checkWork);
		}	
		System.out.println(checkWork.getMorningOutTime());
		System.out.println(checkingSet.getForenoonClosdTime());
		Date dt=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		String time=sf.format(dt)+" "+checkingSet.getForenoonClosdTime();
		Date dt1=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(time);
		if (dt1.after(checkWork.getMorningOutTime())) {
			checkWork.setMorState2(1);//早退
			if (checkWork.getRemark()==null || "".equals(checkWork.getRemark().trim())) {
				checkWork.setRemark("早退");
			}else{
				checkWork.setRemark(checkWork.getRemark()+" "+"早退");
			}
		}else{
			checkWork.setMorState2(0);
		}
		this.getCheckWorkService().submit(checkWork);
		return SUCCESS;
		}
@Action(value="saveCheckWork3", results = {
		@Result(name = "success", type = "redirect", location = "listAttend?inserCode=${checkWork.getCode()}"),
		@Result(name = "error", type = "chain", location = "listAttend?inserCode=${checkWork.getCode()}") })
		public String saveCheckWork3() throws ParseException{
		persons=AppContext.getAccountContext().currentUser().getId();
		Date dt2=new Date();
		SimpleDateFormat sf1=new SimpleDateFormat("yyyyMMdd");
		String codes=sf1.format(dt2);
		this.checkWork=this.getCheckWorkService().getCode(codes,persons);
		if (checkWork!=null) {
			checkWork.setAftState1(0);//正常
			checkWork.setAfternoonInTime(new Date());
			checkWork.setPersons(persons);
			this.getCheckWorkService().save(checkWork);
		}
		Date dt=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		String time=sf.format(dt)+" "+checkingSet.getAfternoonWorkTime();
		Date dt1=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(time);
		if (dt1.after(checkWork.getAfternoonInTime())) {
			checkWork.setAftState1(0);
		}else{
			checkWork.setAftState1(1);//迟到
			if (checkWork.getRemark()==null || "".equals(checkWork.getRemark().trim())) {
				checkWork.setRemark("迟到");
			}else{
				checkWork.setRemark(checkWork.getRemark()+" "+"迟到");
			}
		}
		this.getCheckWorkService().submit(checkWork);
		return SUCCESS;
		}
@Action(value="saveCheckWork4", results = {
		@Result(name = "success", type = "redirect", location = "listAttend?inserCode=${checkWork.getCode()}"),
		@Result(name = "error", type = "chain", location = "listAttend?inserCode=${checkWork.getCode()}") })
		public String saveCheckWork4() throws ParseException{
		persons=AppContext.getAccountContext().currentUser().getId();
		Date dt2=new Date();
		SimpleDateFormat sf1=new SimpleDateFormat("yyyyMMdd");
		String codes=sf1.format(dt2);
		this.checkWork=this.getCheckWorkService().getCode(codes,persons);
		if (checkWork!=null) {
			checkWork.setPersons(persons);
			checkWork.setAftState2(0);
			checkWork.setAfternoonOutTime(new Date());
			this.getCheckWorkService().save(checkWork);
		}
		Date dt=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		String time=sf.format(dt)+" "+checkingSet.getAfternoonClosdTime();
		Date dt1=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(time);
		if (dt1.after(checkWork.getAfternoonOutTime())) {
			checkWork.setAftState2(1);//早退
			if (checkWork.getRemark()==null || "".equals(checkWork.getRemark().trim())) {
				checkWork.setRemark("早退");
			}else{
				checkWork.setRemark(checkWork.getRemark()+" "+"早退");
			}
		}else{
			checkWork.setAftState2(0);
		}
		this.getCheckWorkService().submit(checkWork);
		return SUCCESS;
		}
}
